﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mimoza.AgentLayer
{
    /// <summary>
    /// Create and configure all objects.
    /// </summary>
    public class Configuration
    {
        static Configuration m_globalConfiguration = null;
        
        public Configuration()
        {
            m_globalConfiguration = this;
        }

        public static Configuration GlobalConfiguration
        {
            get { return m_globalConfiguration; }
        }

        public Settings Settings
        {
            get { return m_settings; }
            set { m_settings = value; }
        }

        public void Build()
        {
            if (m_settings != null) return;

            m_settings = new Settings();

            Common.GlobalSettings.InitLicenceFile();

            Mimoza.DBLayer.SQLSC.SQLSC_DBLayer dbLayer =
                new Mimoza.DBLayer.SQLSC.SQLSC_DBLayer();
            dbLayer.DataBaseName = Mimoza.Common.GlobalSettings.AgentDBFileName;
            dbLayer.ConnectToHostDB();

            m_DBLayer = dbLayer;

            m_objectLayer = new Mimoza.ObjectLayer.OL(m_DBLayer);

            m_agentDatabase = new AgentDatabase(this);
            m_agentDatabase.Init();

            m_communicationProcessor = new Communication.ProcessorHostRegistration(this);
            m_hostConnection = new HostConnection(this);

            m_policySynchronizer = new PolicySynchronizer(m_hostConnection, this);
            m_policySynchronizer.Init();
        }

        public void OnInit()
        {
            CreateListener();
            m_hostConnection.StartPeriodicalCreation(Settings.CreateConnectionPeriod);
        }

        public void Deinit()
        {
            m_communicationProcessor.Shutdown();
            m_hostConnection.Shutdown();

            DestroyListeners();
        }

        public void ReStart()
        {
            Deinit();
            OnInit();
        }

        void CreateListener()
        {
            m_communicationProcessor.Init();
        }
                
        void DestroyListeners()
        {
            m_communicationProcessor.Shutdown();
        }

        public ObjectLayer.OL ObjectLayer
        {
            get { return m_objectLayer; }
        }

        public AgentDatabase AgentDatabase
        {
            get { return m_agentDatabase; }
        }

        public PolicySynchronizer PolicySynchronizer
        {
            get { return m_policySynchronizer; }
        }

        public Communication.ProcessorHostRegistration HostRegistration
        {
            get { return m_communicationProcessor; }
        }

        public HostConnection HostConnection
        {
            get { return m_hostConnection; }
        }

        Settings m_settings;
        
        Communication.ProcessorHostRegistration m_communicationProcessor;
        HostConnection m_hostConnection;
        AgentDatabase m_agentDatabase;
        PolicySynchronizer m_policySynchronizer;
        
        Mimoza.Common.IDBLayer m_DBLayer;
        ObjectLayer.OL m_objectLayer;
    }
}
